#-*- coding: utf8 -*-
from tutor.scripts import *
from tutor.plugins.maple import *

# variáveis úteis
half = M('1/2')
t = M.t
_i, _j, _k = M('_i, _j, _k')
rr = M('`..`')
coeff, sqrt = M.coeff, M.sqrt
r = M.r
t = M.t

# parâmetros iniciais
h = oneof(1, 2, 3, 4, 9)
R = sqrt(h)
c1 = oneof(1, 1, -1) * oneof(1, 2, 3, 4)
c2 = oneof(1, 1, -1) * oneof(1, 2, 3, 4)
c3 = oneof(1, 1, -1) * oneof(1, 2, 3, 4)
F = fieldF = sum(v * c * e for v, c, e in zip([x, y, z], [c1, c2, c3], [_i, _j, _k]))
Fx, Fy, Fz = coeff(F, _i), coeff(F, _j), coeff(F, _k)
print('campo:', fieldF)

# superficie inferior
f = x ** 2 + y ** 2
fx = diff(f, x)
fy = diff(f, y)
JS = fx * Fx + fy * Fy - Fz

# fluxo superior
foo = subs(x == r * cos(t), y == r * sin(t), subs(z == h, Fz))
aux = M.int(foo * r, r=rr(0, R))
aux = M.int(aux, t=rr(0, 2 * Pi))
PhiUp = M.simplify(aux)

aux = M.int(foo, r=rr(0, R))
aux = M.int(aux, t=rr(0, 2 * Pi))
PhiUpJ = M.simplify(aux)

aux = M.int(subs(z == h, Fz), x == rr(-R, R))
aux = M.int(aux, y == rr(-R, R))
PhiUpQ = M.simplify(aux)

# fluxo inferior
foo = subs(x == r * cos(t), y == r * sin(t), subs(z == f, JS))
aux = M.int(foo * r, r=rr(0, R))
aux = M.int(aux, t == rr(0, 2 * Pi))
PhiD = M.simplify(aux)

aux = M.int(foo, r=rr(0, R))
aux = M.int(aux, t=rr(0, 2 * Pi))
PhiDJ = M.simplify(aux)

aux = M.int(subs(z == f, JS), x == rr(-R, R))
aux = M.int(aux, y == rr(-R, R))
PhiDQ = M.simplify(aux)

foo = subs(x == r * cos(t), y == r * sin(t), subs(z == f, fx * Fx + fy * Fy + Fz))
aux = M.int(foo * r, r=rr(0, R))
aux = M.int(aux, t=rr(0, 2 * Pi))
PhiDP = M.simplify(aux)

# integral de superfície
Phi = PhiUp + PhiD
PhiParab = PhiD
PhiMix = PhiUp - PhiD
PhiJacob = PhiUpJ + PhiDJ
PhiQuad = PhiUpQ + PhiDQ
PhiElem = PhiDP
print('fluxo: ', Phi)
print('superior: ', PhiParab)
print('misturado: ', PhiMix)
print('jacobiano: ', PhiJacob)
print('quadrado: ', PhiQuad)
print('elemento: ', PhiElem)

